<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2020/12/10
 * Time: 18:47
 */
namespace app\common\widget\message;

use app\common\helper\appmsg_helper;
use app\common\helper\company_helper;
use app\common\helper\open_card_helper;
use app\common\model\Message;

class message_widget
{
    private $company_id;
    private $open_card_id;

    public function __construct()
    {
        $this->open_card_id   = open_card_helper::get_open_card_id();
        $this->company_id     = company_helper::get_company_id();
    }

    /**
     * 添加留言
     * @param array $params
     * @return int
     */
    public function create($params)
    {
        if (!$params || !is_array($params)) {
            return 0;
        }

        $info = array();

        foreach ($params as $k=>$v) {
            if (!is_numeric($v)) {
                $v = filter_string($v);
            }
            $info[$k] = $v;
        }

        if (!isset($info['company_id']) || !$info['company_id']) {
            $info['company_id'] = $this->company_id;
        }

        if (!isset($info['open_card_id']) || !$info['open_card_id']) {
            $info['open_card_id'] = $this->open_card_id;
        }

        if (!isset($info['content']) || !$info['content']) {
            return 0;
        }

        return Message::create($info);
    }
}